package ch.hoth.java.joocal.ui;

import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

import ch.hoth.java.joocal.Calendar;
import ch.hoth.java.joocal.CalendarController;
import ch.hoth.java.joocal.actions.CalendarAction;
import ch.hoth.java.joocal.actions.ChangeDateAction;
import ch.hoth.java.joocal.actions.LoadAction;
import ch.hoth.java.joocal.actions.PublishAction;
import ch.hoth.java.joocal.actions.SaveAction;
import ch.hoth.java.joocal.actions.UpdateAction;

public class MainDialog extends ApplicationWindow{
	
	private CalendarController controller;
	private CTabFolder folder;
	
	private CalendarAction updateAction;
	private CalendarAction saveAction;
	private CalendarAction publishAction;
	
	public MainDialog(CalendarController controller){
		super(null);
		this.controller = controller;

	    addStatusLine();
	    addToolBar(SWT.FLAT);
	}
	
	
	@Override
	protected Control createContents(Composite parent) {
		initialize(parent);
		return folder;
	}

	public void initialize(Composite parent){
		prepareFolder(parent);
		for (Calendar calendar :controller.getCalendars()){
			CalendarWidget widget = new CalendarWidget(folder, calendar);
			controller.addObserver(widget);
		}
		folder.setSelection(folder.getItem(0));
		setStatus("Ready");
	}
	
	/* (non-Javadoc)
	   * @see org.eclipse.jface.window.ApplicationWindow#createStatusLineManager()
	   */
	  protected StatusLineManager createStatusLineManager() {
	    StatusLineManager stlMgr = super.createStatusLineManager();
	    ch.hoth.java.joocal.ui.StatusLineManager.setLineMgr(stlMgr);
	    return stlMgr;
	  }
	  
	  @Override
	  protected ToolBarManager createToolBarManager(int style) {
	    ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT | SWT.BORDER |style);
	   
	    toolBarManager.add(new ChangeDateAction("backward", controller));
	    toolBarManager.add(new ChangeDateAction("today", controller));
	    toolBarManager.add(new ChangeDateAction("forward", controller));
	    toolBarManager.add(new Separator());
	    toolBarManager.add(new LoadAction(controller));
	    saveAction = new SaveAction(controller);
	    toolBarManager.add(saveAction);
	    toolBarManager.add(new Separator());
	    updateAction = new UpdateAction(controller);
	    toolBarManager.add(updateAction);
	    publishAction = new PublishAction(controller);
	    toolBarManager.add(publishAction);
	    return toolBarManager;
	  }
	
	private void prepareFolder(Composite parent){
		folder = new CTabFolder(parent, SWT.BORDER);
		folder.setSimple(false);
		folder.setUnselectedCloseVisible(false);
		folder.setMaximizeVisible(false);
		folder.setMinimizeVisible(false);
		folder.setSelectionForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		folder.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
		folder.setTabHeight(20);
		
		Color[] colors = new Color[2];
	    colors[0] = new Color(null, new RGB(10,36,106));
	    colors[1] = new Color(null, new RGB(166,202,240));
	    folder.setSelectionBackground(colors, new int[]{100}, true);
	    folder.setFont(new Font(Display.getCurrent(), "Nice", 10, SWT.NONE));
	    folder.marginWidth = 100;
	    
	    updateAction.setFolder(folder);
	    saveAction.setFolder(folder);
	    publishAction.setFolder(folder);
	}
}
